package com.cyufeng.blog.service;

import com.cyufeng.blog.dto.ArticleDto;
import com.cyufeng.blog.dto.MoodDto;
import com.cyufeng.blog.entity.Article;
import com.cyufeng.blog.entity.Mood;
import com.cyufeng.blog.entity.Review;
import com.cyufeng.blog.repository.MoodRepository;
import com.cyufeng.blog.service.mapper.MoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 心情
 * @author Cyufeng
 * Data:2018.12.30
 */
@Service
public class MoodService {
    @Autowired
    MoodRepository moodRepository;

    MoodMapper moodMapper = new MoodMapper();

    /**
     * 保存心情
     * @param moodValue
     * @return
     */
    public String saveMood(String moodValue) {
        try {
            Mood mood = new Mood();
            mood.setDetail(moodValue);
            mood.setMadeTime(new Date());
            moodRepository.save(mood);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 得到所有的心情
     * @return
     */
    public List<List<MoodDto>> getAllMood() {
        List<List<MoodDto>> groupMoodList = new ArrayList<>();
        List<MoodDto> tempDtoList = new ArrayList<>();
        List<MoodDto> moodDtoList = new ArrayList<>();
        List<Mood> moodList = moodRepository.getAllMood();
        moodDtoList = moodMapper.entityTodto(moodList);
        //将所有的都分成6个一组
        for (int i = 0, flag = 0; i < moodDtoList.size(); i++) {
            if (flag == 6) {
                groupMoodList.add(tempDtoList);
                flag = 0;
            }

            if (flag == 0) {
                tempDtoList = new ArrayList<>();
            }
            tempDtoList.add(moodDtoList.get(i));
            flag = flag + 1;
        }
        groupMoodList.add(tempDtoList);//把最后一个不满6个的组也加进去
        return groupMoodList;
    }

    public Map<String, Object> getAllMoodPage(Integer pageNum, Integer pageLimit) {
        Pageable pageable = new PageRequest(pageNum - 1, pageLimit);
        Page<Mood> pp = moodRepository.getAllMood(1, pageable);
//        EntityToDto
        List<Mood> moodList = pp.getContent();
        List<MoodDto> moodDtoList = moodMapper.entityTodto(moodList);
        //将所有的都分成6个一组
        List<List<MoodDto>> groupMoodList = new ArrayList<>();
        List<MoodDto> tempDtoList = new ArrayList<>();
        for (int i = 0, flag = 0; i < moodDtoList.size(); i++) {
            if (flag == 6) {
                groupMoodList.add(tempDtoList);
                flag = 0;
            }

            if (flag == 0) {
                tempDtoList = new ArrayList<>();
            }
            tempDtoList.add(moodDtoList.get(i));
            flag = flag + 1;
        }
        groupMoodList.add(tempDtoList);//把最后一个不满6个的组也加进去

        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("content", groupMoodList);//数据
        pageMap.put("totalPages", pp.getTotalPages());//一共有几页
        pageMap.put("totalElements", pp.getTotalElements());//一共有多少条数据
        pageMap.put("pageSize", pp.getPageable().getPageSize());//一页有几条数据
        pageMap.put("pageNumber", pp.getPageable().getPageNumber());//这是第几页
        return pageMap;
    }

    /**
     * 分页查询工具，有条件--用于后台管理
     * @param pageNum
     * @param pageLimit
     * @return
     */
    public Map<String, Object> getAllMood_Back(Integer pageNum, Integer pageLimit) {
        Pageable pageable = new PageRequest(pageNum - 1, pageLimit);
        Page<Mood> pp = moodRepository.getAllMood(1, pageable);
//        EntityToDto
        List<Mood> toolList = pp.getContent();
        List<MoodDto> toolDtoList = moodMapper.entityTodto(toolList);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("content", toolDtoList);//数据
        pageMap.put("totalPages", pp.getTotalPages());//一共有几页
        pageMap.put("totalElements", pp.getTotalElements());//一共有多少条数据
        pageMap.put("pageSize", pp.getPageable().getPageSize());//一页有几条数据
        pageMap.put("pageNumber", pp.getPageable().getPageNumber());//这是第几页
        return pageMap;
    }

    //    删除心情
    @Transactional
    public String deleteMood(int id) {
        try {
            moodRepository.deleteMood(id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 保存心情
     * @param moodDto
     * @return
     */
    public String saveEditMood(MoodDto moodDto) {
        try {
//            将从前台传来的dto通过mapper转为entity再保存entity
            Mood mood = new Mood();
            mood = moodMapper.dtoToentity(moodDto);
            mood.setMadeTime(new Date());
            moodRepository.save(mood);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

}
